<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/27 0027
 * Time: 10:45
 */

namespace app\admin\controller;


use think\Db;
class Admin extends Middle
{
    //todo 管理员列表
    public function adminList(){
        $list = Db::name('admin')
            ->field('aid,account,create_time,log_time,status')
            ->select();
        return $this->returnSuccess($list);
    }

    //todo 添加管理员
    public function AdminAdd(){
        $set = [
            'account'    =>$this->data['account'],
            'password'   =>md5($this->data['password']),
            'create_time'=>time(),
        ];
        $res = Db::name('admin')->insert($set);
        if($res){
            return $this->returnSuccess('添加成功');
        }else{
            return $this->returnError($this->Error);
        }
    }

    //todo 详情
    public function AdminDetail(){
        $map = ['aid'=>$this->data['aid']];
        $res = Db::name('admin')
            ->field('aid,account,create_time,log_time,status')
            ->where($map)
            ->find();
        if($res){
            return $this->returnSuccess($res);
        }else{
            return $this->returnError($this->NoData);
        }
    }
    //todo 修改密码
    public function AdminRePassword(){
        $set = ['aid'=>$this->data['aid'],'password'=>md5($this->data['password']),'update_time'=>time()];
        $res = Db::name('admin')->update($set);
        if($res)return $this->returnSuccess('修改成功');
        return $this->returnError($this->Error);
    }
    //todo 删除管理员
    public function AdminDel(){
        $count = Db::name('admin')->count();
        if($count == 1)return $this->returnError('不能删除最后一个管理员');
        $map = ['aid'=>$this->data['aid']];
        $res = Db::name('admin')->where($map)->delete();
        if($res)return $this->returnSuccess('操作成功');
        return $this->returnError($this->Error);
    }
}